package com.xydpeng.tech.member.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xydpeng.tech.member.dao.MemberInfoMapper;
import com.xydpeng.tech.member.manager.MemberAddressService;
import com.xydpeng.tech.member.manager.MemberInfoService;
import com.xydpeng.tech.member.manager.MemberLevelService;
import com.xydpeng.tech.member.manager.MemberTagService;
import com.xydpeng.tech.member.model.entity.MemberAddress;
import com.xydpeng.tech.member.model.entity.MemberInfo;
import com.xydpeng.tech.member.model.entity.MemberLevel;
import com.xydpeng.tech.member.model.entity.MemberTag;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xydpeng
 * @version v1.0
 * @date: 2024/7/17
 * @description:
 */
@Component
public class TestCommandLineRunner {
    Logger logger = LoggerFactory.getLogger(TestCommandLineRunner.class);

    @Resource
    private MemberInfoService memberInfoService;
    @Resource
    private MemberAddressService memberAddressService;
    @Resource
    private MemberLevelService memberLevelService;
    @Resource
    private MemberTagService memberTagService;
    @Resource
    private MemberInfoMapper memberInfoMapper;

    // @Override
    public void run(String... args) throws Exception {
        for (int i = 0; i < 1; i++) {
            MemberInfo member = new MemberInfo();
            member.setUsername("zhangsan");
            member.setGender(1);
            member.setPassword("123456");
            member.setPhone("12345678910");
            memberInfoService.save(member);
            System.out.println("=======save seccuss========：" + member.getUserId());
            MemberInfo memberInfo = memberInfoService.getById(member.getUserId());
            System.out.println("====select:" + memberInfo);

            Page<MemberInfo> page = memberInfoService.page(new Page<>(1, 10), Wrappers.lambdaQuery(MemberInfo.class).eq(MemberInfo::getUserId, member.getUserId()));
            System.out.println("====list:" + page);
        }

        ArrayList<MemberAddress> list = new ArrayList<>();
        for (int i = 0; i < 1; i++) {
            MemberAddress adress = new MemberAddress();
            adress.setUserId(1020382666781884417L);
            adress.setName("张三" + i);
            adress.setAreaId((long) i);
            adress.setDefaultStatus(false);
            adress.setMobile("11111112222");
            adress.setDetailAddress("北京市朝阳区" + i);
            list.add(adress);
        }
        boolean b = memberAddressService.saveBatch(list);
        System.out.println("saveBatch:" + b);

        Page<MemberAddress> addressPage = memberAddressService.page(new Page<>(2, 2), Wrappers.lambdaQuery(MemberAddress.class).eq(MemberAddress::getUserId, 1020382473915203584L));
        Page<MemberAddress> addressPage2 = memberAddressService.page(new Page<>(2, 2), Wrappers.lambdaQuery(MemberAddress.class).eq(MemberAddress::getUserId, 1020382666781884417L));
        System.out.println(addressPage);

        MemberLevel level = new MemberLevel();
        level.setName("level_1");
        level.setBackgroundUrl("red");
        level.setExperience(100);
        level.setDiscountPercent(98);
        level.setStatus(1);
        memberLevelService.save(level);

        MemberTag tag = new MemberTag();
        tag.setName("职业");
        memberTagService.save(tag);

        //测试关联查询
        List<Map> mapList = memberInfoMapper.selectInfoAndAddress(1020382666781884417L);

        List<Map> mapList1 = memberInfoMapper.selectInfoAndLevel(1020382666781884417L);

        List<Map> mapList2 = memberInfoMapper.selectInfoAndTag(1020377017125699584L);

        List<Map> mapList3 = memberInfoMapper.selectInfoAndTag(1020382666781884417L); //报错Unsupported SQL operation: all tables must be in the same compute node.
        System.out.println("----end---");
    }


    public void test1() throws Exception {
        MemberInfo member = new MemberInfo();
        member.setUsername("zhangsan");
        member.setGender(1);
        member.setPassword("123456");
        member.setPhone("12345678910");
        memberInfoService.save(member);
        System.out.println("=======save seccuss========：" + member.getUserId());
        MemberInfo memberInfo = memberInfoService.getById(member.getUserId());
        System.out.println("====select:" + memberInfo);
    }

    public void test2() throws Exception {
        List<MemberInfo> list = memberInfoService.list(Wrappers.lambdaQuery(MemberInfo.class).eq(MemberInfo::getUsername, "zhangsan"));
        System.out.println("====select:" + list.size());
    }
}
